﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SharedGenomics.Core;
using SharedGenomics.Domain;
using SharedGenomics.Workbench.Core;
using SharedGenomics.Workbench.PresentationModel;

namespace SharedGenomics.Workbench.Views.WindowsForms
{
    public partial class OpenPhenotypeFileView : View, IOpenPhenotypeFileView
    {
        public OpenPhenotypeFileView()
        {
            InitializeComponent();

            this.lbUploadMessage.Text = String.Empty;
            this.btMovePrevious.Click += (o, e) => { this.MovePreviousClicked.Raise(this); };
            this.btUpload.Click += (o, e) => { this.UploadClicked.Raise(this); };
            this.cmbSchemas.SelectedIndexChanged += (o, e) => { this.SelectedFileSchemaChanged.Raise(this); };
        }

        [Region("FileBrowse")]
        public Panel FileBrowsePanel
        {
            get { return this.plFileBrowse; }
        }

        [Region("PhenotypeValidationErrors")]
        public Panel ValidationErrorsPanel
        {
            get { return this.plValidationErrors; }
        }

        private void btLoadSchema_Click(object sender, EventArgs e)
        {
            this.LoadSchemaClicked.Raise(this, EventArgs.Empty);
        }

        private void btAddSchema_Click(object sender, EventArgs e)
        {
            this.AddSchemaClicked.Raise(this);
        }

        public bool IsLoadSchemaEnabled
        {
            set { this.btLoadSchema.Enabled = value; }
        }

        public IEnumerable<EntityInfo> AvailableSchemas
        {
            set
            {
                this.cmbSchemas.DataSource = value;
                this.cmbSchemas.ValueMember = "Id";
                this.cmbSchemas.DisplayMember = "Name";
            }
        }

        public EntityInfo SelectedFileSetInfo
        {
            get { return this.cmbSchemas.SelectedItem as EntityInfo; }
        }

        public event EventHandler LoadSchemaClicked;
        public event EventHandler AddSchemaClicked;
        public event EventHandler MovePreviousClicked;
        public event EventHandler UploadClicked;
        public event EventHandler SelectedFileSchemaChanged;

        public int? SelectedFileSchemaId
        {
            get
            {
                EntityInfo selectedSchemaInfo = this.cmbSchemas.SelectedItem as EntityInfo;
                return selectedSchemaInfo == null
                    ? (int?)null
                    : selectedSchemaInfo.Id;
            }
        }

        public string UploadMessage
        {
            set { this.lbUploadMessage.Text = value; }
        }

        public bool IsUploadEnabled
        {
            set { this.btUpload.Enabled = value; }
        }

        public bool IsMovePreviousEnabled
        {
            set { this.btMovePrevious.Enabled = value; }
        }

        public bool IsUploadProgressBarVisisble
        {
            set { this.pgbUpload.Visible = value; }
        }

        public FileSetUploadPresentationModel PresentationModel
        {
            set { this.bindingSource.DataSource = value; }
        }

        protected override IEnumerable<Binding> ValidatedDataBindings
        {
            get { return this.bindingSource.CurrencyManager.Bindings.Cast<Binding>(); }
        }
    }
}
